/*
 * @author hoang.pt
 * @version $Id$
 */

package snippet.theory2;

/**
 *
 * @author hoang.pt
 */
public abstract class BathItem {

    /**
     * state of this item
     */
    boolean state;

    /**
     * constructor
     */
    public BathItem() {
        //instance of chopstick and first state is state
        this.state = true;

    }

    /**
     * Demo abstract and wrapper
     * @return String
     */
    public abstract String getName();

    /**
     * takeup this item
     * @author hoang.pt
     */
    public synchronized void takeUp() {
        //check state of this item, if availe take up, if not wait
        while(!state) {
            try {
                //System.out.println("This " +getName()+ " is busy");
                wait();
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }

        //change state when takeup
        state = false;
        //take up consume 1 s
        try {
            Thread t = Thread.currentThread();
            System.out.println(t.getName()+ " taking up this " +this.getName()+ ".");
            Thread.sleep(1000);
        } catch(InterruptedException ex) {
            ex.printStackTrace();
        }

    }

    /**
     * putdown
     * @author hoang.pt
     */
    public synchronized void putDown() {
        //finish and putdown consume 1s
        System.out.println(Thread.currentThread().getName()+ " putdown this " +this.getName());
        this.state = true;
        try {
			Thread.sleep(2000);
        } catch(InterruptedException ex) {
            ex.printStackTrace();
        }

        //finish and announce to all
        notifyAll();
    }
}



/**
 * Toothpaste entity
 * @author hoang.pt
 */
class ToothPaste extends BathItem {

    /**
     * constructor
     * @param name
     */
    public ToothPaste() {
    }

    /**
     * $author hoang.pt
     * @return String
     */
    @Override
    public String getName() {

        return "Colgate";
        //throw new UnsupportedOperationException("Not supported yet.");
    }

}



/**
 * ToothCup
 * @author hoang.pt
 */
class ToothCup extends BathItem {

    /**
     * constructor
     */
    public ToothCup() {
    }
    
    /**
     * Override get name
     * @author hoang.pt
     * @return
     */
    @Override
    public String getName() {

        return "Billgate";
        //throw new UnsupportedOperationException("Not supported yet.");
    }

}
